import { CategoryModel } from '~/server/models/category.model';

// 创建分类
export default defineEventHandler(async (event): Promise<any> => {
	try {
		const body = await readBody(event);

		const { name, description } = body;

		if (!name || !description) {
			return { code: 0, message: '分类名称和描述不能为空' };
		}

		const exist = await CategoryModel.findOne({ name });

		if (exist) {
			return { code: 0, message: '分类已存在' };
		}

		await CategoryModel.create({ name, description });

		return { code: 1, message: '创建成功' };
	} catch (error: any) {
		return { code: 0, message: error.message };
	}
});
